<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" /></el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button size="medium" @click="hCancel">取消</el-button>
      <el-button type="primary" size="medium" @click="hSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/employee'
import { addDepartment, getDepartmentInfo, getDepartments, updateDepartmentInfo } from '@/api/department'
export default {
  name: 'VueHrDeptDialog',
  props: {

    id: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    }
  },
  data() {
    const validCode = async(rule, value, callback) => {
      const res = await getDepartments()
      res.data.depts.shift()
      const list = res.data.depts
      let codeList = list.map(item => item.code)
      // console.log(codeList)
      if (this.isEdit) {
        codeList = res.data.depts.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (codeList.includes(value)) {
        callback(new Error('部门编码不能重复'))
      } else {
        callback()
      }
    }
    const validName = async(rule, value, callback) => {
      const res = await getDepartments()
      res.data.depts.shift()
      const list = res.data.depts

      let NameList = list.filter(item => item.pid === this.id).map(item => item.name)

      if (this.isEdit) {
        const curPid = list.find(item => item.id === this.id)
        NameList = list.filter(item => item.pid === curPid.pid && this.id !== item.id).map(item => item.name)
      }
      console.log(NameList)
      // console.log(codeList)
      if (NameList.includes(value)) {
        callback(new Error('同级部门名称不能重复'))
      } else {
        callback()
      }
    }

    return {
      form: {
        manager: '',
        name: '',
        code: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { trigger: 'blur', validator: validName }
        ],
        code: [
          { required: true, message: '请输入部门编号', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { trigger: 'blur', validator: validCode }
        ],
        manager: [
          { required: true, message: '请输入选择负责人', trigger: 'blur' }

        ],
        introduce: [
          { required: true, message: '请输入部门描述', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在 1 到 300个字符', trigger: 'blur' }
        ]
      },
      list: [],
      patrInfo: {}
    }
  },

  mounted() {

  },
  async created() {
    this.loadEmployeeSimple()
    // console.log(this.isEdit)
    if (this.isEdit) {
      this.showPartInfo()
    }
  },
  methods: {
    hCancel() {
      this.$emit('close')
    },
    hSubmit() {
      this.$refs.deptForm.validate(async(valid) => {
        if (!valid) return
        try {
          if (!this.isEdit) {
            this.form.pid = this.pid
            await addDepartment(this.form)
          // console.log(res)
          } else {
            const res = await updateDepartmentInfo(this.form)
            console.log(res)
          }
          this.$emit('success')
        } catch (error) {
          console.log(error)
        }
      })
    },
    async  loadEmployeeSimple() {
      try {
        const res = await getEmployeeSimple()
        // console.log(res)
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    },
    async showPartInfo() {
      try {
        const res = await getDepartmentInfo(this.id)
        // console.log(res)
        this.form = { ...res.data }
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
